package com.itheima.service.impl;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;


    /**
     * 添加检查组
     *
     * @param checkitemIds
     * @param checkGroup
     */
    @Override
    @Transactional
    public void add(String[] checkitemIds, CheckGroup checkGroup) {
        checkGroupMapper.addGroup(checkGroup);

        Integer checkgroupId = checkGroup.getId();
        checkGroupMapper.addItem(checkgroupId, checkitemIds);
    }

    /**
     * 条件分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Integer start = (currentPage - 1) * pageSize;

        List<CheckGroup> list = checkGroupMapper.selectPage(start, pageSize, queryString);
        Long total = checkGroupMapper.selectTotal(queryPageBean);

        return new PageResult(total, list);
    }

    /**
     * 根据id删除检查组
     * @param id
     */
    @Override
    @Transactional
    public void delete(String id) {
        // 根据检查组id查询检查项id
        List<Integer> checkItemIds = checkItemMapper.findCheckItemId(Integer.valueOf(id));
        if (checkItemIds != null && checkItemIds.size() > 0) {
            // 删除检查项和检查组的关联关系
            checkGroupMapper.deleteItem(id);
        }

        // 删除检查组
        checkGroupMapper.delete(id);
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     */
    @Override
    @Transactional
    public void edit(String[] checkitemIds, CheckGroup checkGroup) {
        //修改检查组表信息
        checkGroupMapper.update(checkGroup);

        //修改关联表信息
        // 1.删除检查项和检查组的关联关系
        checkGroupMapper.deleteItem(checkGroup.getId().toString());
        // 2.重新添加检查项和检查组的关联关系
        checkGroupMapper.addItem(checkGroup.getId(), checkitemIds);
    }

    /**
     *  根据检查组id查询检查组
     * @param checkgroupId
     * @return
     */
    @Override
    public CheckGroup findById(String checkgroupId) {
        CheckGroup checkGroup = checkGroupMapper.findById(checkgroupId);
        return checkGroup;
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> selectAll() {
        List<CheckGroup> list = checkGroupMapper.selectAll();
        return list;
    }

    /**
     * 根据套餐id查询关联的检查组id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckGroupId(String setmealId) {
        List<Integer> checkGroupIds = checkGroupMapper.findCheckGroupId(setmealId);
        return checkGroupIds;
    }
}
